﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Ingenious.Utility
{
    /// <summary>
    /// Weak event manager for property changed events
    /// </summary>
    public class PropertyChangedEventManager : WeakEventManager
    {
        public static PropertyChangedEventManager CurrentManager
        {
            get
            {
                Type type = typeof(PropertyChangedEventManager);
                PropertyChangedEventManager manager = WeakEventManager.GetCurrentManager(type) as PropertyChangedEventManager;

                if (manager == null)
                {
                    manager = new PropertyChangedEventManager();
                    WeakEventManager.SetCurrentManager(type, manager);
                }

                return manager;
            }
        }

        public static void AddListener(INotifyPropertyChanged source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedAddListener(source, listener);
        }

        public static void RemoveListener(INotifyPropertyChanged source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedRemoveListener(source, listener);
        }

        protected override void StartListening(object source)
        {
            ((INotifyPropertyChanged)source).PropertyChanged += DeliverEvent;
        }

        protected override void StopListening(object source)
        {
            ((INotifyPropertyChanged)source).PropertyChanged -= DeliverEvent;
        }
    }
}
